package com.stedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Account;
import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.AdminService;
import com.stedu.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Value("${pageSize}")
    private Integer pageSize;
    //分页
    @GetMapping
    public RespBean selectByPage(Integer pageNum, String aname){
        if (pageNum == null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, pageSize);

        List<Admin> anameAll = adminService.selectByAname(aname);

        PageInfo<Admin> pageInfo = new PageInfo<>(anameAll, 5);

        return RespBean.ok("查询成功", pageInfo);
    }

    @PostMapping
    public RespBean insertController(@RequestBody Admin admin) throws SteduException {
        if( adminService.insertAdmin(admin)){
            return RespBean.ok("添加成功");
        }else{
            return RespBean.error("添加失败");
        }
    }

    //删除
    @DeleteMapping("/{aid}")
    public RespBean deleteController(@PathVariable Integer aid){
        if (adminService.deleteAdmin(aid)){
            return RespBean.ok("删除成功");
        }else{
            return RespBean.error("删除失败");
        }
    }

    //修改
    @PutMapping
    public RespBean updateController(@RequestBody Admin admin){
        if (adminService.updateAdmin(admin)){
            return RespBean.ok("修改成功");
        }else{
            return RespBean.error("修改失败");
        }
    }

    //根据姓名查询
    @GetMapping("/ByAname")
    public RespBean selectByAnameController(@RequestBody String aname){
        if (adminService.selectByAname(aname) != null){
            List<Admin> unameAll = adminService.selectByAname(aname);
            return RespBean.ok("查询成功", unameAll);
        }else{
            return  RespBean.error("查询不到,请确定姓名");
        }
    }

    //根据ID查询
    @GetMapping("/ByAid/{aid}")
    public RespBean selectByAidController(@PathVariable Integer aid){
        if (adminService.selectByAid(aid) != null){
            Admin admin = adminService.selectByAid(aid);
            return RespBean.ok("查询成功", admin);
        }else {
            return RespBean.error("查询失败,没有该用户");
        }
    }

    //查询所有
    @GetMapping("/All")
    public RespBean selectAllController(){
        List<Admin> admins = adminService.selectAll();
        return RespBean.ok("查询成功",admins);
    }



}
